package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import pojo.Product;
import service.ProductManager;
import service.impl.ProductManagerFactory;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/sec/update")
public class ProductUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ProductUpdateServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Logger logger = Logger.getLogger(ProductUpdateServlet.class);

		String wid = request.getParameter("ww");
		String pid = (String) request.getSession().getAttribute("uuid");
		
		System.out.println("session中取出的pid： "+pid);
		System.out.println("最后的更新： "+ pid);
		
		String pname = request.getParameter("pname");
		String pprice = request.getParameter("pprice");
		String pcount = request.getParameter("pcount");
		String pdate = request.getParameter("pdate");

		// /校验：

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		boolean isOk = true;
		if (pname == null || pname.trim().length() == 0) {
			request.setAttribute("err_pname", "货品名为空");
			isOk = false;
		} else {
			request.setAttribute("err_pname", null);
		}
		if (pprice == null || pprice.trim().length() == 0) {
			request.setAttribute("err_pname", "货品价格为空");
			isOk = false;
		} else if (pprice.length() > 8) {
			request.setAttribute("err_pname", "货品价格不能大于8位");
			isOk = false;
		} else {
			request.setAttribute("err_pname", null);
		}

		if (pcount == null || pcount.trim().length() == 0) {
			request.setAttribute("err_pname", "货品数量不能为空，至少为1");
			isOk = false;
		}

		if (!isOk) {
			request.getRequestDispatcher("update.jsp").forward(request,
					response);
			return;
		}

		Product p = new Product();
		double price = Double.parseDouble(pprice);
		int count = Integer.parseInt(pcount);
		Date date = null;
		try {
			date = format.parse(pdate);
		} catch (ParseException e) {

			e.printStackTrace();
			request.setAttribute("err_pdate", "日期格式有误");
			isOk = false;
		}

		p.setId(pid);
		p.setName(pname);
		p.setPrice(price);
		p.setCount(count);
		p.setPd(date);
		p.setWid(wid);

		System.out.println("即将更新传来的Product: "+ p.toString());
		if(ProductManagerFactory.getInstance().updateProduct(p)>0){
		System.out.println("Product更新");
		request.setAttribute("curw", wid);
		request.getRequestDispatcher("listupdate").forward(request, response);
		}
	}

}
